<?php

namespace App\Exceptions;

use App\Exceptions\Interfaces\AddressServiceException;
use App\Exceptions\Interfaces\CartServiceException;
use App\Exceptions\Interfaces\OrderServiceException;
use App\Exceptions\Interfaces\UserServiceException;
use EasyWeChat\Kernel\Exceptions\HttpException;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\Finder\Exception\AccessDeniedException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
        AddressException::class,

        ExpressException::class,
        CartServiceException::class,
        AddressServiceException::class,
        OrderServiceException::class,
        UserServiceException::class,
        \App\Exceptions\Interfaces\SmsSendErrorException::class,


    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {

        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if ($exception instanceof CartServiceException||$exception instanceof  CartExceptions) {

            $return['code'] = 0;
            $return['message'] = $exception->getMessage();
            return Response()->json($return);

        }//end if()

        if($exception instanceof  AuthorizationException){

            if(strstr($request->url(),'myTeam')){

                return \Response::view('mobile.myTeam.tip');

            }
            $return['code']=0;
            $return['message']='你没有权限进行此操作';
            return Response()->json($return);
        }
        if($exception instanceof  OrderServiceException){
            $return['code'] = 0;
            $return['message'] = $exception->getMessage();
            return Response()->json($return);

        }
        if($exception instanceof  AddressServiceException){
            $return['code'] = 0;
            $return['message'] = $exception->getMessage();
            return Response()->json($return);

        }

        if($exception instanceof  HttpException){
            $return['code'] = 0;
            $return['message'] = $exception->getMessage();
            return Response()->json($return);

        }
        if($exception instanceof  ExpressException){
            $return['code'] = 0;
            $return['message'] = $exception->getMessage();
            return Response()->json($return);
        }
        if($exception instanceof  SmsSendErrorException){
            $return['code'] = 0;
            $return['message'] = $exception->getMessage();
            return Response()->json($return);
        }
            return parent::render($request, $exception);

    }
}
